package com.gf.controller.shop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gf.bean.Shop;
import com.gf.service.impl.ShopServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/*
* 展示所有店铺的servlet
* */

@WebServlet(name = "allshop",urlPatterns = "/selectallshop")
public class SelectallShopServlet extends HttpServlet {
    static ShopServiceImpl shopimpl = null;
    static {
        shopimpl=new ShopServiceImpl();


    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {






    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        //创建打印输出对象
        JSONObject backjson = new JSONObject();
        //新建要输出的json对象
        List<Shop> shops = shopimpl.selectAll();
        //接收返回的所有店铺对象
        //打印正确的状态码和对应的数据
        backjson.put("state", 200);
        backjson.put("data", shops);
        out.println(JSON.toJSON(backjson));
        //打印字符串格式的json对象并输出
        out.flush();
        out.close();


    }
}
